// pages/mine/arrange.js
var app = getApp();
var api = require('../../api.js');
var util = require('../../utils/util.js');
var WxParse = require('../../utils/wxParse/wxParse.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shop: [],
    shop_index: 0,
    project: [],
    project_index: 0,
    num: 1,
    date: '2019-09-01',
    time: '10:00',
  },
  shop_change: function(e) {
    var index = e.detail.value;
    this.setData({
      shop_index: index,
    })
  },
  project_change: function(e) {
    var index = e.detail.value;
    this.setData({
      project_index: index,
    })
  },
  bind_num: function(e) {
    this.setData({
      num: e.detail.value,
    })
  },
  bindDateChange: function(e) {
    this.setData({
      date: e.detail.value
    })
  },
  bindTimeChange: function(e) {
    this.setData({
      time: e.detail.value
    })
  },
  formSubmit: function(e) {
    if (wx.getStorageSync('real_name')) {
      var info = {
        shop: this.data.shop[this.data.shop_index],
        project: this.data.project[this.data.project_index],
        num: e.detail.value.num,
        date: e.detail.value.date,
        time: e.detail.value.time,
      };
      console.log(info)
      if (info.num < 1) {
        wx.showToast({
          title: '请输入预约人数至少为一个',
          icon: 'none'
        })
      } else {

        app.ajax(api.arrange_add, {
          shop_id: this.data.shop0[this.data.shop_index].id,
          project_id: this.data.project0[this.data.project_index].id,
          people: info.num,
          date: info.date,
          time: info.time,
          rd_session: wx.getStorageSync('rd_session')
        }, (res) => {
          wx.showToast({
            title: '预约成功！',
            icon: 'none',
            duration: 0,
            mask: true,
            success: (res) => {
              wx.navigateTo({
                url: '/pages/arrange/list',
              })
            },
          })
        });
      }
    } else {
      wx.navigateTo({
        url: '/pages/login/login',
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    //获取当前时间
    var timestamp = Date.parse(new Date());
    var date = new Date(timestamp);
    //获取年份  
    var Y = date.getFullYear();
    //获取月份  
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
    //获取当日日期 
    var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
    // 获取数据
    app.ajax(api.shop_list, {
      rd_session: wx.getStorageSync('rd_session')
    }, (res) => {
      var shop0 = res.data.shop;
      var project0 = res.data.project;
      var content = res.data.introduce.content;
      WxParse.wxParse('article', 'html', content, this, 5);

      var shop_list = [],
        project_list = [];
      for (var i = 0; i < shop0.length; i++) {
        shop_list.push(shop0[i].shop_name)
      }
      for (var i = 0; i < project0.length; i++) {
        project_list.push(project0[i].title)
      }
      this.setData({
        date: Y + '-' + M + '-' + D,
        shop: shop_list,
        shop_index: 0,
        project: project_list,
        project_index: 0,
        project0: project0,
        shop0: shop0
      })

    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})